import plotly.graph_objects as go
# Create the histogram with 10-year bins for age and set the color to red
fig = go.Figure()
fig.add_trace(go.Histogram(
x=df['age'], # Age data
nbinsx=10, # 10-year bins
marker=dict(color='red'), # Set bar color to red for emphasis
histnorm='percent', # Display values as percentages
name='Heart Disease Percentage'
))
# Customize the hover template to show only the bin information
fig.update_traces(
hovertemplate='Age Range: %{x}<br>Heart Disease Likelihood: %{y:.2f}%', # Display age range and percentage
xbins=dict(size=10) # Bin size (10-year intervals)
)
# Update layout for clear appearance and styling
fig.update_layout(
title=dict(
text="Heart Disease Likelihood by Age",
font=dict(size=20, color="black", family="Arial", weight="bold"),
x=0.5, # Center-align the title
xanchor="center"
),
xaxis=dict(
title="Age",
title_font=dict(size=14,weight="bold"),
tickmode="linear",
dtick=10,
showline=True, # Show X-axis line
linecolor="black", # Set X-axis line color to black
linewidth=2 # Set line width for emphasis
),
yaxis=dict(
title=None, # Remove Y-axis title
showticklabels=False # Hide Y-axis values
),
plot_bgcolor="white",
bargap=0.2 # Increase space between bars for clear visibility
)
fig.show()